package com.lft.juc05.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable {
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		// new Thread(new Runnable()).start();
		// new Thread(new FutureTask(Callable)).start();
		
		MyThread2 myThread = new MyThread2();
		// Callable 的适配类 FutureTask
		FutureTask<String> futureTask = new FutureTask<>(myThread);
		new Thread(futureTask,"A").start();
		new Thread(futureTask,"B").start(); // 有缓存。只会操作一个。
		// 获取 Callable 的返回结果，get()方法可能会产生阻塞！因为需要等待返回结果。把他放到最后，或者使用异步通信来处理。
		System.out.println(futureTask.get());
	}
}

class MyThread implements Runnable {
	
	@Override
	public void run() {}
}

class MyThread2 implements Callable<String> {
	
	@Override
	public String call() {
		System.out.println("call()");
		// 耗时操作
		return "null";
	}
}